//
//  MainViewController.m
//  TakeHer
//
//  Created by Jupiter on 15/6/10.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MainViewController.h"
#import "CycleScrollView.h"
#import "FirstMyCell.h"
#import "MyReusableView.h"

#import "FirstDetailViewController.h"
#import "AnotherDetailViewController.h"
@interface MainViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic, retain)NSString *str;
@property(nonatomic, retain)NSArray *titleArr;
@property(nonatomic, retain)UICollectionView *collectionView;
@property (nonatomic , retain) CycleScrollView *mainScorllView;
@property (nonatomic, retain)NSMutableArray *sourceArr;
@end

@implementation MainViewController


-(void)dataHandle
{
    self.str = @"http://api.breadtrip.com/destination/v3/?last_modified_since=0";
    // 对字符串进行UTF8转码
    _str = [_str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    // 创建对象
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manger GET:_str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.titleArr = [responseObject objectForKey:@"elements"];
        
        
        [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        // 打印错误信息
        //        NSLog(@"%@", error);
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//********************上方视图代码部分*************************//
//上方滚动图片
    [self dataHandle];
    
    
    UIScrollView *mainPageScrollerView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, APP_WIDTH, APP_HEIGHT)];
    
    mainPageScrollerView.contentSize = CGSizeMake(0, 600);
    mainPageScrollerView.bounces = NO;
    [self.view addSubview:mainPageScrollerView];
    [mainPageScrollerView release];
    
    NSMutableArray *viewsArray = [@[] mutableCopy];
    NSArray *imageArray = [NSArray arrayWithObjects:
                           @"1.jpg",
                           @"2.jpg",
                           @"3.jpg",
                           @"4.jpg",
                           @"5.jpg",
                           @"6.jpg",
                           @"7.jpg",nil];
    
    for (int i = 0; i < 7; ++i) {
        UIImageView *tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,APP_WIDTH, APP_HEIGHT/ 3 )];
        tempImageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
        [viewsArray addObject:tempImageView];
        
    }
    
    self.mainScorllView = [[CycleScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200) animationDuration:3];
    self.mainScorllView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
    
    self.mainScorllView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        return viewsArray[pageIndex];
    };
    self.mainScorllView.totalPagesCount = ^NSInteger(void){
        return 5;
    };
    self.mainScorllView.TapActionBlock = ^(NSInteger pageIndex){
        NSLog(@"点击了第%ld个",(long)pageIndex);
    };
    [self.view addSubview:self.mainScorllView];
    


    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    //规定每个cell的大小
    flowLayout.itemSize = CGSizeMake(APP_WIDTH / 2.3, APP_HEIGHT / 4);
    //滚动的方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //在一行里面,item之间的最小间距
    flowLayout.minimumInteritemSpacing = 20;
    flowLayout.minimumLineSpacing = 10;
    
    //1.设置sectionHeader的第一步:对flowLayout进行设置
    
    flowLayout.headerReferenceSize = CGSizeMake(APP_WIDTH, 50);
    
    
   self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake((APP_WIDTH - (APP_WIDTH / 2.3 * 2 + 20)) / 2, self.mainScorllView.frame.origin.y+self.mainScorllView.frame.size.height,APP_WIDTH / 2.3 * 2 + 20 , APP_HEIGHT - 300) collectionViewLayout:flowLayout];
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView release];
    [_collectionView registerClass:[FirstMyCell class] forCellWithReuseIdentifier:@"reuse"];
    [_collectionView registerClass:[MyReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    

}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
   
    if ([kind isEqualToString: UICollectionElementKindSectionHeader]) {
        MyReusableView *reuseableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        reuseableView.mylabel.text = [[self.titleArr objectAtIndex:indexPath.section] objectForKey:@"title"];
        return reuseableView;
    }
    return nil;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.titleArr.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[[self.titleArr objectAtIndex:section] objectForKey:@"data"] count];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (0 == indexPath.section) {
        
        FirstDetailViewController *firstDetailView = [[FirstDetailViewController alloc]init];
        
        firstDetailView.str = [[[[self.titleArr objectAtIndex:indexPath.section] objectForKey:@"data"] objectAtIndex:indexPath.item]objectForKey:@"cover"];
        
        firstDetailView.titleDic = [[[self.titleArr objectAtIndex:indexPath.section]objectForKey:@"data"]objectAtIndex:indexPath.item];
        [self.navigationController pushViewController:firstDetailView animated:YES];
        return;
    }
    
    AnotherDetailViewController *anotherDetailView = [[AnotherDetailViewController alloc]init];
    NSDictionary *dic = [[[self.titleArr objectAtIndex:indexPath.section] objectForKey:@"data"] objectAtIndex:indexPath.row];
    anotherDetailView.dic = dic;
    
    
//    anotherDetailView.str =[[[[self.titleArr objectAtIndex:indexPath.section] objectForKey:@"data"] objectAtIndex:indexPath.item]objectForKey:@"cover"];
//    
//    anotherDetailView.titleDic = [[[self.titleArr objectAtIndex:indexPath.section]objectForKey:@"data"]objectAtIndex:indexPath.item];
//    
//
    
    [self.navigationController pushViewController:anotherDetailView animated:YES];
    [anotherDetailView release];
    
    
    
    
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FirstMyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
  [cell.myImageView setImageWithURLStr:[[[[self.titleArr objectAtIndex:indexPath.section] objectForKey:@"data"] objectAtIndex:indexPath.item]objectForKey:@"cover"]];
    cell.myImageView.layer.cornerRadius = 10.0f;
    cell.myImageView.clipsToBounds = YES;
    
    cell.myLabel.text =[[[[self.titleArr objectAtIndex:indexPath.section] objectForKey:@"data"] objectAtIndex:indexPath.item]objectForKey:@"name"];
    //系统提供的cell不带任何空间,如果想使用cell显示信息,必须自定义
    
    
   // cell.myLabel.text = [self.sourceArr objectAtIndex:indexPath.row];
    
    
    
    return cell;
    
}
- (void)dealloc
{
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
